package com.swind.fast.modules.app.space.rent.listener;

import com.swind.fast.common.util.oConvertUtils;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.scheduling.annotation.Async;
import org.springframework.context.event.EventListener;

import org.springframework.context.ApplicationEvent;

import com.swind.fast.modules.app.space.rent.component.SpaceComponent;

import com.swind.fast.modules.app.space.rent.event.SpaceContractCreateEvent;
import com.swind.fast.modules.app.space.rent.entity.SpaceContract;
import com.swind.fast.modules.app.space.rent.entity.SpaceContractBill;
import com.swind.fast.modules.app.space.rent.service.ISpaceContractBillService;
import com.swind.fast.modules.app.space.rent.entity.SpaceCustomer;
import com.swind.fast.modules.app.space.rent.service.ISpaceCustomerService;
import com.swind.fast.modules.app.space.rent.entity.SpaceRegister;
import com.swind.fast.modules.app.space.rent.service.ISpaceRegisterService;
import com.swind.fast.modules.app.space.rent.entity.SpaceRoom;
import com.swind.fast.modules.app.space.rent.service.ISpaceRoomService;


import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class SpaceContractListener {

    // @Autowired
    // private SpaceComponent spaceComponent;

    @Autowired
    private ISpaceContractBillService spaceContractBillService;

    @Autowired
    private ISpaceCustomerService spaceCustomerService;

    @Autowired
    private ISpaceRoomService spaceRoomService;

    @Autowired
    private ISpaceRegisterService spaceRegisterService;

    // @Autowired
    // private RailwayComponent railwayComponent;

    // @Autowired
    // private IRailwayProjectService railwayProjectService;

    // @Autowired
    // private IRailwayProjectOperationLogService railwayProjectOperationLogService;

    // @Async
    @EventListener
    public void spaceContractCreateListener(SpaceContractCreateEvent event) {
        log.info("spaceContractCreateListener");

        SpaceContract contract = event.getContract();
        log.info("contract: {}", contract);
        spaceContractBillService.saveBatchBill(contract);

        String customerId = contract.getCustomerId();
        UpdateWrapper updateWrapper = new UpdateWrapper<SpaceCustomer>().set("status", "1").eq("id", customerId);
        spaceCustomerService.update(updateWrapper);

        String roomId = contract.getRoomId();
        updateWrapper = new UpdateWrapper<SpaceRoom>().set("status", "1").eq("id", roomId);
        spaceRoomService.update(updateWrapper);

        String registerId = contract.getRegisterId();
        updateWrapper = new UpdateWrapper<SpaceRegister>().set("status", "1").eq("id", registerId);
        spaceRegisterService.update(updateWrapper);

        // String billPrice = spaceComponent.getBillPrice(contract);
        // String price = "";
        // String endDate = "";
        // SpaceContractBill bill = SpaceContractBill.builder()
        //                             .contractId(contract.getId())
        //                             .customerId(contract.getCustomerId())
        //                             .roomId(contract.getRoomId())
        //                             .price(price)
        //                             .startDate(contract.getPayDate())
        //                             .endDate(endDate)
        //                             .build();
        // log.info("bill: {}, price: {}, months: {}", bill, spaceComponent.getBillPrice(contract), spaceComponent.getMonthPeriod(contract));

        // RailwayProjectStatusVo projectStatus = (RailwayProjectStatusVo) event.getSource();
        // String projectId = projectStatus.getProjectId();
        // ProjectStatusEnum status = projectStatus.getProjectStatusEnum();

        // RailwayProject project = railwayProjectService.getById(projectId);
        // Integer fstatus = project.getStatus();
        // Integer pstatus = status.getValue();
        // log.info("project: {}, status: {}", project, pstatus);

        // // UpdateWrapper<RailwayProject> itemWrapper = new UpdateWrapper<RailwayProject>().set("status", pstatus).eq("id", id);
        // // railwayProjectService.update(itemWrapper);

        // project.setStatus(pstatus);
        // railwayProjectService.updateById(project);

        // String memo = projectStatus.getMemo();
        // String createBy = projectStatus.getCreateBy();
        // RailwayProjectOperationLog operationLog = new RailwayProjectOperationLog(projectId, fstatus, pstatus, memo, createBy);
        // railwayProjectOperationLogService.save(operationLog);

        // // log.info("username: {}", railwayComponent.getUserRealname());
    }

}
